#include "DB_MySql.h"


/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
MySql_DB::MySql_DB(string v_strServer,string v_strUser,string v_strPassword )
{
      m_strServer    = v_strServer;
      m_strUser      = v_strUser;
      m_strPassword  = v_strPassword;

      //pthread_mutex_init(&m_mutex_MySql , NULL);
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::ConnectToServer(string v_strDataBase)
{
    // pthread_mutex_lock(&m_mutex_MySql);

     if( mysql_init(&m_sql) == NULL )
       {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
    //     pthread_mutex_unlock(&m_mutex_MySql);
         return false;
       }
      if(mysql_set_character_set(&m_sql,"UTF8") != 0)
      {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
       //  pthread_mutex_unlock(&m_mutex_MySql);
         return false;
      }
       if (!mysql_real_connect(&m_sql, m_strServer.c_str(),m_strUser.c_str(), m_strPassword.c_str(), v_strDataBase.c_str(), 0, NULL, 0))
      {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
       //  pthread_mutex_unlock(&m_mutex_MySql);
         return false;
      }

     // pthread_mutex_unlock(&m_mutex_MySql);
    LOG(INFO) << "connect to mysql server database :" << v_strDataBase;
//    InsertToTableUser("zhangbing","abc","3344");
//    JudgeLogin("zhangbing","abcn");
    return true;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::InsertToTableUser(string v_strUserName ,
                                 string v_strPassword ,
                                 string v_strDeviceID)
{
     //pthread_mutex_lock(&m_mutex_MySql);
 string str_Sql =  "insert into User values (";

        str_Sql += "NULL";
        str_Sql += ",";

        str_Sql += "'";
        str_Sql += v_strUserName;
        str_Sql += "'";
        str_Sql += ",";

        str_Sql += "'";
        str_Sql += v_strPassword;
        str_Sql += "'";
        str_Sql += ",";

        str_Sql += "'";
        str_Sql += v_strDeviceID;
        str_Sql += "'";

        str_Sql += ");";

 if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
       //  pthread_mutex_unlock(&m_mutex_MySql);
         LOG(ERROR) << str_Sql ;
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return false;
    }
  //  pthread_mutex_unlock(&m_mutex_MySql);
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::JudgeLogin(string v_strUserName ,string v_strPassword )
{
  string str_Sql =  "select * from User ";

        str_Sql += "where UserName = ";

        str_Sql += "'";
        str_Sql += v_strUserName;
        str_Sql += "'";

        str_Sql += " AND Password = ";
        str_Sql += "'";
        str_Sql += v_strPassword;
        str_Sql += "';";
    //pthread_mutex_lock(&m_mutex_MySql);
    if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         LOG(ERROR) << str_Sql ;
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
    //      pthread_mutex_unlock(&m_mutex_MySql);
         return false;
    }

    //获取结果集

    if (!(m_pRes = mysql_store_result(&m_sql)))    //获得sql语句结束后返回的结果集
    {
         LOG(ERROR) << str_Sql ;
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
       //    pthread_mutex_unlock(&m_mutex_MySql);
         return false;
    }
    if(mysql_affected_rows(&m_sql) < 1)
    {
      LOG(INFO) << "UserName Or Password Error";
     //  pthread_mutex_unlock(&m_mutex_MySql);
      return false;
    }
   // pthread_mutex_unlock(&m_mutex_MySql);
    return true;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::GetOrder(string v_strState,ORDER_LIST &OrderList)
{
  string str_Sql =  "select * from orderform ";

        str_Sql += "where state = ";

        str_Sql += v_strState;

        str_Sql += ";";
   // pthread_mutex_lock(&m_mutex_MySql);
    if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
          LOG(ERROR) << str_Sql ;
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
        //  pthread_mutex_unlock(&m_mutex_MySql);
         return false;
    }

    //获取结果集
    if (!(m_pRes = mysql_store_result(&m_sql)))    //获得sql语句结束后返回的结果集
    {
         LOG(ERROR) << str_Sql ;
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
//         pthread_mutex_unlock(&m_mutex_MySql);
         return false;
    }

    int i = 0;
    int nOrderNumber = mysql_affected_rows(&m_sql);

    for(i = 0 ; i < nOrderNumber ; i++)
    {
       OrderForm order;
       m_Row = mysql_fetch_row(m_pRes);
       printf("%10s\t%10s\t%10s\t%10s\n", m_Row[0], m_Row[1], m_Row[2],m_Row[3]);  //column是列数组
             order.strOrderID   = m_Row[orderId];
             order.strTime      = m_Row[ordertime];
             order.strNumber    = m_Row[number];
             order.strLength    = m_Row[length];
             order.strWidth     = m_Row[width];
             order.strThickness = m_Row[thickness];
             order.strThreshold = m_Row[threshold];

             OrderList.push_back(order);

             SetOrderStateById(order.strOrderID , OrderState_WaitForProcess);
    }
//    pthread_mutex_unlock(&m_mutex_MySql);
    return true;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::SetOrderStateById(string v_strOrderId ,string v_strState)
{
 string str_Sql =  "update orderform set state = ";

        str_Sql += v_strState;

        str_Sql += " where orderId = ";

        str_Sql += v_strOrderId;

        str_Sql += ";";
  //  pthread_mutex_lock(&m_mutex_MySql);
    if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         LOG(ERROR) << str_Sql ;
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
//         pthread_mutex_unlock(&m_mutex_MySql);
         return false;
    }
//        pthread_mutex_unlock(&m_mutex_MySql);
    return true;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::GetDevice(string v_strState,DEVICE_LIST &DeviceList)
{
  string str_Sql =  "select * from device ";

         str_Sql += "where deviceState = ";

         str_Sql += v_strState;

         str_Sql += ";";
    //pthread_mutex_lock(&m_mutex_MySql);
    if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         LOG(ERROR) << str_Sql ;
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
//         pthread_mutex_unlock(&m_mutex_MySql);
         return false;
    }

    //获取结果集
    if (!(m_pRes = mysql_store_result(&m_sql)))    //获得sql语句结束后返回的结果集
    {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
//         pthread_mutex_unlock(&m_mutex_MySql);
         return false;
    }

    int i = 0;
    int nNumberOfOrder = mysql_affected_rows(&m_sql);
    printf("NumberOfFreeOrder   : %d\r\n",nNumberOfOrder);
    for(i = 0 ; i < nNumberOfOrder ; i++)
    {
            DeviceNode device;
            m_Row = mysql_fetch_row(m_pRes);

            device.DeviceID       = m_Row[0];
            device.DeviceState    = m_Row[1];
            string strConnectFd   = m_Row[2];

            device.new_fd = atoi(strConnectFd.c_str());

            SetDeviceStateById(device.DeviceID , DeviceState_Wait);

            DeviceList.push_back(device);
//                pthread_mutex_unlock(&m_mutex_MySql);
    }
//    pthread_mutex_unlock(&m_mutex_MySql);
    return true;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::SetDeviceStateById(string v_strDevcieId ,string v_strState)
{
 string str_Sql =  "update device set deviceState = ";

        str_Sql += v_strState;

        str_Sql += " where deviceId = ";

        str_Sql += v_strDevcieId;

        str_Sql += ";";
   // pthread_mutex_lock(&m_mutex_MySql);
    if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << str_Sql ;
         LOG(ERROR) << mysql_error(&m_sql) ;
//         pthread_mutex_unlock(&m_mutex_MySql);
         return false;
    }
//        pthread_mutex_unlock(&m_mutex_MySql);
    return true;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::SetDeviceConnectFdById(string v_strDevcieId ,string v_strConnectFd)
{
 string str_Sql =  "update device set ConnectFd = ";

        str_Sql += v_strConnectFd;

        str_Sql += " where deviceId = ";

        str_Sql += v_strDevcieId;

        str_Sql += ";";
   // pthread_mutex_lock(&m_mutex_MySql);
    if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
//         pthread_mutex_unlock(&m_mutex_MySql);
         return false;
    }
//        pthread_mutex_unlock(&m_mutex_MySql);
    return true;
}

/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
string MySql_DB::GetThresholdById(string v_strOrderId)
{
  string str_Sql =  "select * from orderform ";

         str_Sql += " where orderId = ";

         str_Sql += v_strOrderId;

         str_Sql += ";";
 //   pthread_mutex_lock(&m_mutex_MySql);
    if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
//         pthread_mutex_unlock(&m_mutex_MySql);
         return "";
    }

    //获取结果集
    if (!(m_pRes = mysql_store_result(&m_sql)))    //获得sql语句结束后返回的结果集
    {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
//         pthread_mutex_unlock(&m_mutex_MySql);
         return "";
    }

    int nNumber = mysql_affected_rows(&m_sql);

    if(nNumber != 1)
    {
       LOG(ERROR) << "Can not find OrderId" ;
//       pthread_mutex_unlock(&m_mutex_MySql);
       return "";
    }

    m_Row = mysql_fetch_row(m_pRes);
//    pthread_mutex_unlock(&m_mutex_MySql);
    string strThreshold = m_Row[threshold];

    return strThreshold;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
MySql_DB::~MySql_DB()
{
  //  pthread_mutex_destroy(&m_mutex_MySql);
    mysql_free_result(m_pRes);
    mysql_close(&m_sql);
}
